﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using CodeLibrary.Consts;
using CodeLibrary.IO.Utils.FileDialog;

namespace CodeLibrary.IO.Utils
{
    public static class DirectoryUtils
    {
        public static void CopyDirectory(string sourceDirName, string destDirName, bool copySubDirs)
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(HintConsts.DirectoryNotFound);
            }

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, false);
            }

            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    CopyDirectory(subdir.FullName, temppath, true);
                }
            }
        }

       
        private static string SelectDirectoryByCommonDialog(string initialDirectory)
        {
            IFileOpenDialog iFolderSelector = new WinAPI_FileOpenDialog() as IFileOpenDialog;
            if (iFolderSelector == null)
            {
                throw new NullReferenceException("");
            }
            iFolderSelector.SetOptions(FOS.FOS_PICKFOLDERS);
            Guid guid = new Guid(ShellIIDGuid.IShellItem2);
            IShellItem2 defaultDirectoryShellItem;
            ShellNativeMethods.SHCreateItemFromParsingName(initialDirectory, IntPtr.Zero, ref guid,
                                                                        out defaultDirectoryShellItem);
            iFolderSelector.SetDefaultFolder(defaultDirectoryShellItem);
            iFolderSelector.SetFolder(defaultDirectoryShellItem);
            string sFileName;
            iFolderSelector.Show(Process.GetCurrentProcess().MainWindowHandle);
            IShellItem iShellResult;
            iFolderSelector.GetResult(out iShellResult);
            iShellResult.GetDisplayName(SIGDN.FILESYSPATH, out sFileName);
            return sFileName;
        }

        public static string SelectDirectory(string initialDirectory)
        {
            string result = "";
            try
            {
                result = SelectDirectoryByCommonDialog(initialDirectory);
            }
            catch (NullReferenceException)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog {SelectedPath = initialDirectory};
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    result = dialog.SelectedPath;
                }
            }
            return result;
        }

        public static string SelectDirectory()
        {
            return SelectDirectory("");
        }

        public static void DeleteDirectory(string directory)
        {
            if (Directory.Exists(directory))
            {
                Directory.Delete(directory, true);
            }
        }

        public static void BrowseDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                throw new DirectoryNotFoundException();
            }
            Process.Start(directory);
        }
    }
}
